package com.cnswhy.cloud.server.merchant.controller.iot;

import com.alibaba.fastjson.JSON;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.mq.CreateTerminalMQ;
import com.cnswhy.cloud.server.core.entity.IotChargingLineIndexInfo;
import com.cnswhy.cloud.server.core.dto.IotChargingLineIndexInfoSaveDTO;
import com.cnswhy.cloud.server.core.dto.IotChargingLineIndexInfoUpdateDTO;
import com.cnswhy.cloud.server.core.dto.IotChargingLineIndexInfoPageDTO;
import com.cnswhy.cloud.server.core.entity.IotTerminalApplyInfo;
import com.cnswhy.cloud.server.core.service.IotChargingLineIndexInfoService;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.server.merchant.dto.IotTerminalApplyInfoVerifyDTO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
//import com.github.zuihou.security.annotation.PreAuth;
import org.springframework.web.bind.annotation.RestController;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2021-03-12
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/iotChargingLineIndexInfo")
@Api(value = "IotChargingLineIndexInfo", tags = "")
//@PreAuth(replace = "iotChargingLineIndexInfo:")
public class IotChargingLineIndexInfoController extends SuperController<IotChargingLineIndexInfoService, Long, IotChargingLineIndexInfo, IotChargingLineIndexInfoPageDTO, IotChargingLineIndexInfoSaveDTO, IotChargingLineIndexInfoUpdateDTO> {
    @PostMapping("/rebuild")
    public R<Boolean> rebuld() {
        List<IotChargingLineIndexInfo> iotChargingLineIndexInfos = baseService.list();
        for(IotChargingLineIndexInfo iotChargingLineIndexInfo : iotChargingLineIndexInfos){
            log.info(iotChargingLineIndexInfo.getTerminalId());
            String[] aCodeList = iotChargingLineIndexInfo.getTerminalId().split("\\|");
            int index = Integer.valueOf(aCodeList[0]);
            iotChargingLineIndexInfo.setIndexId(index);
            String aIndex = "";
            String bIndex = "";
            String cIndex = "";
            for(int i = 1 ;i <= 25 ;i ++){
                aIndex = aIndex + aCodeList[i] + "|";
            }
            for(int i = 26 ;i <= 50 ;i ++){
                bIndex = bIndex + aCodeList[i] + "|";
            }
            for(int i = 51 ;i <= 75 ;i ++){
                cIndex = cIndex + aCodeList[i] + "|";
            }
            iotChargingLineIndexInfo.setAIndex(aIndex);
            iotChargingLineIndexInfo.setBIndex(bIndex);
            iotChargingLineIndexInfo.setCIndex(cIndex);
            iotChargingLineIndexInfo.setResetCode(aCodeList[76]);
            baseService.updateById(iotChargingLineIndexInfo);
        }
        return success();
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<IotChargingLineIndexInfo> iotChargingLineIndexInfoList = list.stream().map((map) -> {
            IotChargingLineIndexInfo iotChargingLineIndexInfo = IotChargingLineIndexInfo.builder().build();
            return iotChargingLineIndexInfo;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(iotChargingLineIndexInfoList));
    }
}
